﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using BCEntity.OA.TeamWork.TeamWorkCategory;
using BCCommon;
namespace BCData.OA.TeamWork.TeamWorkCategory
{
    /// <summary>
    /// 协同办公分类
    /// </summary>
	public class TeamWorkCategoryData : ITeamWorkCategoryData
    {
        private const string QUERY_EXISTS = @"SELECT 1 FROM OA_TeamWorkCategory";
        private const string QUERY = @"SELECT TeamWorkCategoryId,TeamWorkCategoryCode,TeamWorkCategoryName,CreateTime,CreateAdminId,CreateAdminName,EditTime,EditAdminId,EditAdminName,
                                            IsPredefined
                                        FROM OA_TeamWorkCategory";

        private readonly IDatabaseContext databaseContext;

        public TeamWorkCategoryData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<bool> Exists(int teamWorkCategoryId)
        {
            string sql = string.Format(@"{0} WHERE TeamWorkCategoryId=?TeamWorkCategoryId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkCategoryId", teamWorkCategoryId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<bool> Exists(string teamWorkCategoryCode)
        {
            string sql = string.Format(@"{0} WHERE TeamWorkCategoryCode=?TeamWorkCategoryCode", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkCategoryCode", teamWorkCategoryCode?.Trim(), DbType.AnsiString);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<TeamWorkCategoryEntity> Add(TeamWorkCategoryEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("OA_TeamWorkCategory", new string[] { "TeamWorkCategoryCode", "TeamWorkCategoryName", "CreateAdminId", "CreateAdminName" });
            sql = string.Format("{0} {1} WHERE TeamWorkCategoryId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkCategoryCode", entity.TeamWorkCategoryCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?TeamWorkCategoryName", entity.TeamWorkCategoryName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<TeamWorkCategoryEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<TeamWorkCategoryEntity> Update(TeamWorkCategoryEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("OA_TeamWorkCategory", new string[] { "TeamWorkCategoryCode", "TeamWorkCategoryName", "CreateAdminId", "CreateAdminName",
                "EditTime", "EditAdminId", "EditAdminName" }, new string[] { "TeamWorkCategoryId" });
            sql = string.Format("{0} {1} WHERE TeamWorkCategoryId=?TeamWorkCategoryId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkCategoryId", entity.TeamWorkCategoryId, DbType.Int32, size: 11);
            parameters.Add("?TeamWorkCategoryCode", entity.TeamWorkCategoryCode?.Trim(), DbType.AnsiString, size: 50);
            parameters.Add("?TeamWorkCategoryName", entity.TeamWorkCategoryName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?EditTime", DateTime.Now, DbType.DateTime);
            parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
            parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<TeamWorkCategoryEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<bool> Delete(int teamWorkCategoryId)
        {
            string sql = @"DELETE FROM OA_TeamWorkCategory WHERE TeamWorkCategoryId=?TeamWorkCategoryId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkCategoryId", teamWorkCategoryId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<TeamWorkCategoryEntity> GetEntity(int teamWorkCategoryId)
        {
            string sql = string.Format(@"{0} WHERE TeamWorkCategoryId=?TeamWorkCategoryId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkCategoryId", teamWorkCategoryId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<TeamWorkCategoryEntity>(sql, parameters);
        }

        public async Task<TeamWorkCategoryEntity> GetEntity(string teamWorkCategoryCode)
        {
            string sql = string.Format(@"{0} WHERE TeamWorkCategoryCode=?TeamWorkCategoryCode", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?TeamWorkCategoryCode", teamWorkCategoryCode, DbType.AnsiString);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<TeamWorkCategoryEntity>(sql, parameters);
        }

        public async Task<IEnumerable<TeamWorkCategoryEntity>> GetList()
        {
            return await this.databaseContext.Slave.QueryAsync<TeamWorkCategoryEntity>(QUERY);
        }

        public async Task<(IEnumerable<TeamWorkCategoryEntity>, int)> Query(string teamWorkCategoryName, string teamWorkCategoryCode, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(teamWorkCategoryName))
            {
                wheres.Add("TeamWorkCategoryName LIKE CONCAT('%',CONCAT(?TeamWorkCategoryName,'%'))");
                parameters.Add("?TeamWorkCategoryName", teamWorkCategoryName?.Trim(), DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(teamWorkCategoryCode))
            {
                wheres.Add("TeamWorkCategoryCode=?TeamWorkCategoryCode");
                parameters.Add("?TeamWorkCategoryCode", teamWorkCategoryCode?.Trim(), DbType.AnsiString);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY CreateTime DESC LIMIT {2},{3}; SELECT COUNT(1) FROM OA_TeamWorkCategory {1}",
                   QUERY,
                   wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                   (pageIndex - 1) * pageSize,
                   pageSize);
            return await this.databaseContext.Slave.QueryPageableListAsync<TeamWorkCategoryEntity>(sql, parameters);
        }
    }
}
